'''
Author: your name
Date: 2020-10-20 13:42:04
LastEditTime: 2020-10-20 16:49:15
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: /mtl-text-recognition/config/Train_Ticket_Clean.py
'''
import random
import string
import pypinyin
import cv2
import math
import os
def open_txt(file_name):
    with open(file_name, 'r') as f:
        try:
            line = f.readline()
            while line:
                yield line.strip()
                line = f.readline()
        except:
            print('No value')

li9933 = [str(line) for line in open_txt('/data1/lhw/workspace/demo/Text_Anolization/9933_L.txt')]
ligbk = [str(line) for line in open_txt('/data1/lhw/workspace/demo/Text_Anolization/GBK_txt.txt')]
lien = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
          'A', 'B', 'C', 'D',
          'E',
          'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a',
          'b',
          'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
          'y',
          'z']
all_dict = ligbk + lien + ['\'', ':', '.', '¥', '*']
# print(all_dict)

listation = [str(line) + '站' for line in open_txt('/data1/lhw/workspace/mtl-text-recognition/config/stations.txt')]
# print(listation)

ticket_no = ''.join(random.sample(string.ascii_letters + string.digits, 9)).upper()
# print(ticket_no)

def convert_to_pinyin(text):
    pinyin = pypinyin.lazy_pinyin(text)
    pinyin[0] = pinyin[0].capitalize()
    return "".join(pinyin)

# print(convert_to_pinyin('沾益站'))


def random_train_no():
    rtn = list()
    rtn.append(random.sample(["GC", "D", "Z", "T", "K"], 1)[0])
    for i in range(random.randint(2, 4)):
        rtn.append(random.sample(string.digits.replace("0", "") if i == 0 else string.digits, 1)[0])
    return "".join(rtn)
# print(random_train_no())


def random_id_no():
    id_no = ""
    for i in range(18):
        if 9 < i < 14:
            id_no += "*"
        else:
            id_no += random.sample(string.digits if i != 0 else string.digits.replace("0", ""), 1)[0]
    return id_no
# print(random_id_no())
# txt_path = '/data1/mjh/datasets/train_ticket_v1/new_ano'
# img_path = '/data1/mjh/datasets/train_ticket_v1/images'
# count = 0
# labels = open('/data1/mjh/datasets/train_ticket_v1/line/labels.txt', 'w')
# for file in os.listdir(txt_path):
#     name = file.strip()[:-4]
#     # print(name)
#     img_file = os.path.join(img_path, name+'.jpg')
#     img = cv2.imread(img_file)
#     print(img_file)
#     txt_file = os.path.join(txt_path, name+'.txt')

#     for line in open_txt(txt_file):
#         # print(line)
#         xmin, ymin, xmax, ymax, gt = line.split(',')
#         xmin, ymin, xmax, ymax = int(xmin), int(ymin), int(xmax), int(ymax)
#         tmp = img[ymin-5:ymax+10, xmin-5:xmax+5]
#         # print(tmp.shape)
#         h, w = ymax - ymin, xmax - xmin
#         ratio = 32/h
#         w_new = math.ceil(w*ratio)
#         tmp = cv2.resize(tmp, (w_new, 32),interpolation=cv2.INTER_CUBIC)
#         print(tmp.shape)
#         out_img = '/data1/mjh/datasets/train_ticket_v1/line/{}.png'.format(str(count))
#         cv2.imwrite(out_img, tmp)
#         labels.writelines(out_img + '\t' + gt.strip() + '\r\n')
#         count += 1
    
txt_path = '/data2/dtj/DTJCOMP/ppccddff/train_labeled.txt'
out = open('/data2/dtj/DTJCOMP/ppccddff/train_ticket.txt', 'w')
not_in = ['中国', '12306', '铁路', '祝', '您', '中', '票']

count = 16000
for line in open_txt(txt_path):
    try:
        if count > 0:
            img, gt = line.split('\t')
            new_img = img[:6] + '/dtj' + img[6:]
            # print(new_img)
            gt = gt.strip()
            tmp = True
            for c in not_in:
                if c in gt:
                    tmp = False
            if '￥' in gt:
                gt[0] = '¥'
            if tmp:
                # print(gt)
                out.writelines(new_img + '\t' + gt + '\r\n')
        count -= 1
    except:
        pass
    
